package com.yipao.db.service.goods;

import com.github.pagehelper.PageHelper;
import com.yipao.db.dao.goods.GoodsMapper;
import com.yipao.db.domain.goods.Goods;
import com.yipao.db.domain.goods.Goods.Column;
import com.yipao.db.domain.goods.vo.goods.GoodsAdminListReq;
import com.yipao.db.domain.goods.vo.goods.GoodsExample;
import com.yipao.db.domain.goods.vo.goods.GoodsAdminListResp;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品
 */
@Service
public class GoodsService {
    Column[] columns = new Column[]{Column.id, Column.name, Column.brief, Column.picUrl, Column.isHot, Column.isNew, Column.counterPrice, Column.retailPrice, Column.tags, Column.formulaId};
    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<Goods> queryByHot(int offset, int limit) {
        GoodsExample example = new GoodsExample();
        example.or().andIsHotEqualTo(true).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return goodsMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<Goods> queryByNew(int offset, int limit) {
        GoodsExample example = new GoodsExample();
        example.or().andIsNewEqualTo(true).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return goodsMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<Goods> queryByCategory(List<Integer> catList, int offset, int limit) {
        GoodsExample example = new GoodsExample();
        example.or().andCategoryIdIn(catList).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        example.setOrderByClause("add_time  desc");
        PageHelper.startPage(offset, limit);

        return goodsMapper.selectByExampleSelective(example, columns);
    }


    /**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    public List<Goods> queryByCategory(Integer catId, int offset, int limit) {
        GoodsExample example = new GoodsExample();
        example.or().andCategoryIdEqualTo(catId).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return goodsMapper.selectByExampleSelective(example, columns);
    }


    /**
     * 查询指定设备下的商品列表
     *
     * @param categoryId 分类id
     * @param keywords
     * @param offset
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<Goods> querySelectiveByDevId(Integer devId, Integer categoryId, String keywords, Integer offset, Integer limit, String sort, String order) {
        String orderByClause = null;
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            orderByClause = sort + " " + order;
        }
        PageHelper.startPage(offset, limit);
        return goodsMapper.querySelectiveByDevId(devId, categoryId, keywords,
                orderByClause, DeleteStatusConstant.NOT_DELETED, Goods.GOODS_IN_STATUS);
    }

    public List<Goods> querySelective(Integer catId, String keywords, Integer offset, Integer limit, String sort, String order) {
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria1 = example.createCriteria();
        GoodsExample.Criteria criteria2 = example.createCriteria();

        if (!StringUtils.isEmpty(catId) && catId != 0) {
            criteria1.andCategoryIdEqualTo(catId);
            criteria2.andCategoryIdEqualTo(catId);
        }

        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS);
        criteria2.andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS);
        criteria1.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        criteria2.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(offset, limit);

        return goodsMapper.selectByExampleSelective(example, columns);
    }

    public List<GoodsAdminListResp> querySelective(Integer goodsId, String goodsSn, String name, Integer page, Integer size, String sort, String order) {
        String orderByClause = sort + " " + order;

        PageHelper.startPage(page, size);
        List<GoodsAdminListResp> goodsAdminListResps = goodsMapper.selectByExampleWithBLOBs(
                new GoodsAdminListReq(goodsId,
                        goodsSn, name, orderByClause,
                        DeleteStatusConstant.NOT_DELETED));
        return goodsAdminListResps;
    }

    /**
     * 获取某个商品信息,包含完整信息
     *
     * @param id
     * @return
     */
    public Goods findById(Integer id) {
        GoodsExample example = new GoodsExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return goodsMapper.selectOneByExampleWithBLOBs(example);
    }

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public Goods findByIdVO(Integer id) {
        GoodsExample example = new GoodsExample();
        example.or().andIdEqualTo(id).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return goodsMapper.selectOneByExampleSelective(example, columns);
    }


    /**
     * 获取所有在售物品总数
     *
     * @return
     */
    public Integer queryOnSale() {
        GoodsExample example = new GoodsExample();
        example.or().andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return (int) goodsMapper.countByExample(example);
    }

    public int updateById(Goods goods) {
        goods.setUpdateTime(LocalDateTime.now());
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    public void deleteById(Integer id) {
        goodsMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(Goods goods) {
        goods.setAddTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goodsMapper.insertSelective(goods);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     *
     * @return
     */
    public int count() {
        GoodsExample example = new GoodsExample();
        example.or().andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return (int) goodsMapper.countByExample(example);
    }

    public List<Integer> getCatIds(String keywords) {
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria1 = example.or();
        GoodsExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS);
        criteria2.andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS);
        criteria1.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        criteria2.andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);

        List<Goods> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for (Goods goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    }

    public boolean checkExistByName(String name) {
        GoodsExample example = new GoodsExample();
        example.or().andNameEqualTo(name).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return goodsMapper.countByExample(example) != 0;
    }

    public List<Goods> queryByIds(Integer[] ids) {
        GoodsExample example = new GoodsExample();
        example.or().andIdIn(Arrays.asList(ids)).andIsOnSaleEqualTo(Goods.GOODS_IN_STATUS).andDeletedEqualTo(DeleteStatusConstant.NOT_DELETED);
        return goodsMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 查询指定设备绑定的商品列表
     *
     * @param devId 设备id
     * @return
     */
    public List<Goods> queryDevGoodsByDevId(String devId) {
        return goodsMapper.queryDevGoodsByDevId(devId, DeleteStatusConstant.NOT_DELETED);
    }

    public Object add(GoodsAdminListResp vo) {
        return goodsMapper.add(vo);
    }


    /**
     * 根据配方id,查询商品
     *
     * @param formulaId 配方id
     * @return
     */
    public int queryCountGoodsByFormulaId(Integer formulaId) {
        return goodsMapper.queryCountGoodsByFormulaId(formulaId, DeleteStatusConstant.NOT_DELETED);
    }
}
